import {reqGetCartList, reqDeleteCartById, reqUpdateIsCheckedById} from "@/api";
import {getUuid} from '@/utils/uuid_token'
//Home模块
const state = {
    cartList: [],
    //临时游客身份号
    uuid_token: getUuid()
}

const actions = {
    async getCartList({commit}) {
        let result = await reqGetCartList()
        if (result.code === 200) {
            commit('GETCARTLIST', result.data)
        }
    },
    async deleteCartById({commit}, skuId) {
        let result = await reqDeleteCartById(skuId)
        if (result.code === 200) {
            return 'success'
        } else {
            return Promise.reject(new Error('fail to deleteCartById'))
        }
    },
    async updateIsCheckedById({commit}, {skuId, isChecked}) {
        let result = await reqUpdateIsCheckedById(skuId, isChecked)
        if (result.code === 200) {
            return 'success'
        } else {
            return Promise.reject(new Error('fail to updateIsCheckedById'))
        }
    },
}

const mutations = {
    GETCARTLIST(state, cartList) {
        state.cartList = cartList
    }
}

const getters = {
    cartInfoList(state) {
        return state.cartList[0] === undefined ? [] : state.cartList[0].cartInfoList || []
    }
}//为了简化仓库中的数据

export default {
    namespaced: true,
    state,
    actions,
    mutations,
    getters
}

